angular.module('starter.controllers', [])
    .controller('MainCtrl', function ($rootScope, $scope, $window) {
        $rootScope.window = $window;
        $rootScope.jqueryInit = $scope.jquryInit = function () {
            $(function () {
                $('*[data-toggle="tooltip"]').tooltip();
                $('*[rel=popover]').popover();


            })
        }



        $rootScope.loading = false;
    })
    .controller('HomeCtrl', function ($rootScope, $scope, $timeout, $http, Orders, Main, $window) {



        $scope.endTime = getDay(1);

        $scope.filerCondition = '';
        $scope.days = [];
        $scope.orders = [];



        var saveOrder = function (data) {
            localStorage.setItem('orderInfo', JSON.stringify(data.orderInfo));
            localStorage.setItem('days', JSON.stringify(data.days));
            data.orders.forEach(function (i) {
                i.addressd = stripTags(i.addressd);
                i.add_time = new Date(i.add_timed);
                i.address = JSON.parse(i.address);
                Main.DB.insert(i).then(function (res) {
                    // console.log('saveOrder Data ' + (res));
                },function(err){
                    console.log(err);
                })
            })
        }

        $scope.getOrderFromSite = function (getOrderFromSiteCallBack) {
            var getOrderFromSiteCallBack = getOrderFromSiteCallBack || null;
            var t = {'start': '', 'end': $scope.endTime};

            console.log('getOrderFromSite sss' + (new Date()));

            Orders.orders(t).then(function (data) {

                console.log('getOrderFromSite Data ' + (data));

                saveOrder(data);
                if (getOrderFromSiteCallBack)
                    getOrderFromSiteCallBack(data);
            }, function (data) {
                $rootScope.loadingText = '网络连接错误';
                setTimeout(function () {
                    $scope.$apply(function () {
                        $rootScope.loading = false;
                    })
                }, 2000);

            });
        }


        $scope.getOrderFromDb = function (getOrderFromDbCallBack) {
            var getOrderFromDbCallBack = getOrderFromDbCallBack || null;


            console.log('getOrderFromDb ' + (new Date()));


            Main.DB.getAll().then(function (data) {
                var orderInfo = JSON.parse(localStorage.getItem('orderInfo'));
                var d = {orders: data, orderInfo: orderInfo}
                $scope.setOrders(d);

                if (getOrderFromDbCallBack)
                    getOrderFromDbCallBack(data)

            })

        }

        $scope.setOrders = function (data) {
            console.log('setOrders');

            $scope.orders = groupByDate(data.orders);
            $scope.days = data.days;
            $scope.orderInfo = data.orderInfo;
            $rootScope.loading = false;
        }

         $scope.doRefresh = function (date) {

            $rootScope.loading = true;
            $scope.endTime = date;
            $scope.getOrderFromSite($scope.setOrders);

        };
        $scope.checkOrders = function (orders) {

            console.log('checkOrders ' + (orders.length));
            if (orders.length == 0) {
                $rootScope.loading = true;
                $scope.doRefresh('')
            }
            else
                $rootScope.loading = false;

        }

        $scope.setOrderCondition = function (obj) {
            $scope.filerCondition = obj;
        }
        console.log('home ' + (new Date()));
        $scope.getOrderFromDb($scope.checkOrders);
        //Main.refresh('.refresh',$scope.doRefresh);

    })

    .controller('CreateCtrl', function ($scope, Main, DB) {
        Main.dbInit();
        $scope.user = {};
        $scope.validate = function () {

            DB.save($scope.user).then(function (res) {
                console.log(res);
            });

        }

    })
    .controller('UpdateCtrl', function ($scope, Friends) {
        $scope.friends = Friends.all();
    })

    .controller('ViewCtrl', function ($scope, Main, $stateParams) {
        Main.loadingShow();
        var order_sn = '00' + parseInt($stateParams.id);
        $scope.order = {};
        Main.DB.find('order_sn', order_sn).then(function (res) {
            console.log('View Data');
            $scope.order = res;
            Main.loadingHide();
        });


    })

    .controller('DeleteCtrl', function ($scope) {
    })
    .controller('ClearCtrl', function ($rootScope,Main, $scope, dbname) {
        $rootScope.loading = false;
        Main.DB.clear();
        var res = window.indexedDB.deleteDatabase(dbname);

        $scope.message = '重置成功';
        // console.log(res);

    });
;
